package com.xiaomi.service.Impl;

import com.xiaomi.dao.CartDao;
import com.xiaomi.dao.GoodsDao;
import com.xiaomi.pojo.Cart;
import com.xiaomi.pojo.Goods;
import com.xiaomi.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName CartServiceImpl
 * @Author xiao宋
 * @Date 15:25  2022/9/14
 * @Version 1.0
 * @Description TODO
 */
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    CartDao cartDao;
    @Autowired
    GoodsDao goodsDao;

    @Override
    public Cart findCartByUidAndGoodsId(int uid, int goodsId) {
        return cartDao.findCartByUidAndGoodsId(uid, goodsId);
    }

    @Override
    public void insert(Cart cart) {
         cartDao.insert(cart);
    }

    @Override
    public void update(Cart cart) {
         cartDao.update(cart);
    }

    @Override
    public List<Cart> getCarts(int uid) {
        List<Cart>carts= cartDao.getCart(uid);

        if (carts.size()>0){
            for (Cart cart : carts) {
                int pid=cart.getPid();
                Goods goods = goodsDao.findById(pid);
                cart.setGoods(goods);
            }
        }
        return carts;
    }

    @Override
    public void deleteCart(int goodsId, int uid) {
        cartDao.deleteCart(uid,goodsId);
    }

    @Override
    public void clearCart(int uid) {
        cartDao.clearCart(uid);
    }
}
